import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/routers/jump_router.dart';

class H5Page extends StatefulWidget {
  final String? url;
  final String? title;
  final bool showAppbar;

  const H5Page({super.key, this.url, this.title, this.showAppbar = true});

  @override
  State<StatefulWidget> createState() {
    return _H5PageState();
  }
}

class _H5PageState extends State<H5Page> {
  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webView;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true, //使用混合集成
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        bool back = await webView?.canGoBack() ?? false;
        if (back == true) {
          webView?.goBack();
          return false;
        }
        return true;
      },
      child: Scaffold(
          appBar: widget.showAppbar ? AppBar(title: Text(widget.title ?? '')) : null,
          body: SafeArea(
              child: Stack(
            children: [
              InAppWebView(
                key: webViewKey,
                initialUrlRequest: URLRequest(url: Uri.parse(widget.url!)),
                initialUserScripts: UnmodifiableListView<UserScript>([]),
                onWebViewCreated: (controller) {
                  webView = controller;
                  controller.addJavaScriptHandler(
                      handlerName: 'prizeBack',
                      callback: (arg) {
                        Get.back();
                      });
                  webView?.addJavaScriptHandler(
                    handlerName: 'shopBack',
                    callback: (arguments) {
                      Get.back();
                    },
                  );

                  webView?.addJavaScriptHandler(
                    handlerName: 'shopCharge',
                    callback: (arguments) {
                      /// 跳转到金币充值
                      jumpToWalletPage(tabPosition: 1);
                    },
                  );
                },
                onLoadStart: (InAppWebViewController? controller, Uri? url) async {
                  setState(() {
                    //this.url = url;
                  });

                  debugPrint("onPageFinished$url");
                },
              ),
              if (!widget.showAppbar)
                Positioned(
                  child: InkWell(
                    onTap: () => Get.back(),
                    child: Image.asset(
                      'search_close.png'.commonImgPath,
                      width: 30,
                    ),
                  ),
                  right: 16,
                  top: 20,
                ),
            ],
          ))),
    );
  }
}
